package com.zte.sys.position.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.sys.dept.model.Dept;
import com.zte.sys.login.model.Permission;
import com.zte.sys.modules.model.MModule;
import com.zte.sys.position.dao.IJobDao;
import com.zte.sys.position.model.Job;
import com.zte.sys.position.model.JobCondition;
import com.zte.sys.position.service.IJobService;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JobService implements IJobService {

    @Autowired
    private IJobDao jobDao;

    /**
     * 职位分页条件查询
     * @param conn
     * @return
     */
    @Override
    public PageModel<Job> queryJobPageByCondition(JobCondition conn,PageModel<Job> pageModel) {
        if (conn.getJobNamelike().equals("null")){
            conn.setJobNamelike("");
        }
        if (conn.getJobDeptId().equals("null") ){
            conn.setJobDeptId("");
        }
        int pageNum = pageModel.getPageNum();
        pageNum = pageNum<=0?1:pageNum;
        int pageSize = 3;
        PageHelper.startPage(pageNum,pageSize);
        List<Job> jobs = jobDao.queryJobListByCondition(conn);
        PageInfo<Job> info = new PageInfo<Job>(jobs);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalPage(info.getPages());
        pageModel.setTotalCount((int)info.getTotal());
        pageModel.setData(new ArrayList<Job>(info.getList()));
        return pageModel;
    }

    /**
     * 职位条件查询
     * @param conn
     * @return
     */
    @Override
    public List<Job> queryJobListByCondition(JobCondition conn) {
        return jobDao.queryJobListByCondition(conn);
    }

    @Override
    public List<Dept> queryDeptList(String deptId) {
        return jobDao.queryDeptList(deptId);
    }

    /**
     * 职位详情
     * @param jobId
     * @return
     */
    @Override
    public Job queryJobByJobId(String jobId) {
        return jobDao.queryJobByJobId(jobId);
    }

    /**
     * 添加职位
     * @param job
     * @return
     */
    @Override
    public int addJob(Job job) {
        job.setJobState("1");
        return jobDao.addJob(job);
    }

    /**
     * 修改职位
     * @param job
     * @return
     */
    @Override
    public int updateJob(Job job) {
        return jobDao.updateJob(job);
    }

    /**
     * 注销恢复修改
     * @param job
     * @return
     */
    @Override
    public int updateJobState(Job job) {
        if(job.getJobState().equals("2")){
            job.setJobState("1");
        }else {
            job.setJobState("2");
        }
        return jobDao.updateJobState(job);
    }

//    public List<MModule> queryModuleByJobId(JobCondition conn){
//        return jobDao.queryModuleByJobId(conn);
//    }

    /**
     * 权限module查询
     * @param conn
     * @return
     */
    public Map<String,List<MModule>> queryModuleMap(JobCondition conn){
        Map<String,List<MModule>> map = new HashMap<>();
        List<MModule> mModules = jobDao.queryModuleByModulePid(conn);
        System.out.println(mModules);
        for (int i = 0; i < mModules.size(); i++) {
            if(mModules.get(i).getModulePid().equals("0") && mModules.get(i).getModuleState().equals("1")){
                List<MModule> list = new ArrayList<>();
                for (int j = 0; j < mModules.size(); j++) {
                    if (mModules.get(j).getModulePid().equals(mModules.get(i).getModuleId()) && mModules.get(j).getModuleState().equals("1")){
                        list.add(mModules.get(j));
                    }
                }
                map.put(mModules.get(i).getModuleId()+" "+mModules.get(i).getModuleRname(),list);
            }
        }
        return map;
    }

    /**
     * 删除所有权限
     * @param comrid
     * @return
     */
    @Override
    public int delPermission(String comrid) {
        return jobDao.delPermission(comrid);
    }

    /**
     * 重新赋权
     * @param permission
     * @return
     */
    @Override
    public int addPermission(Permission permission) {
        permission.setComstate(1);
        return jobDao.addPermission(permission);
    }
}
